import struct

import numpy as np
import matplotlib.pyplot as plt

def read_raw(path):
    with open(path, 'rb') as f:
        data = f.read()
    w_byte = data[:4]
    h_byte = data[4:8]
    im = data[8:]
    w = struct.unpack('I', w_byte)[0]
    h = struct.unpack('I', h_byte)[0]
    dst = np.zeros((h, w), np.uint8)

    for i in range(h):
        for j in range(w):
            dst[i][j] = im[i*w + j]
    return dst


def cal_hist(src):
    hist = np.zeros(256, dtype=int)  # 先赋值一个全零数组，用来统计各个灰度级像素个数
    h, w = src.shape[:2]  # 取图像矩阵的高和宽
    for i in range(h):
        for j in range(w):
            hist[src[i][j]] += 1  # 统计
    return hist


path = 'D:/Downloads/bb/FILE16.raw'
im = read_raw(path)
# hist = cal_hist(im)
# for i in range(1, 256):
#     hist[i] += hist[i-1]
# # hist = hist / hist.max()
# plt.figure()
# plt.bar(np.arange(0, 256), hist)
# plt.show()
print(im)

